import threading
import time


class Singleton:
    __instance = None
    __lock = threading.RLock()

    def __init__(self, name):
        self.name = name

    def __new__(cls, *args, **kwargs):
        """
        with cls.__lock:
            if not cls.__instance:
                cls.__instance = object.__new__(cls)
            return cls.__instance
        -->优化(完美)
        """
        if cls.__instance:
            return cls.__instance

        with cls.__lock:
            cls.__instance = object.__new__(cls)
            return cls.__instance


def task():
    obj = Singleton('x')
    print(obj,id(obj))


def test():
    for i in range(100):
        t = threading.Thread(target=task())
        t.start()
